package com.fm.webreader.html;

import com.fm.base.struct.DoubleLinkedList;
import com.fm.base.struct.DoubleLinkedNode;
import com.fm.base.util.Pair;
import com.fm.commons.util.StringUtil;
import com.fm.webreader.html.table.HtmlTable;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class InfoReader {
	private static String bracket_l = "(\\(|（)";
	private static String bracket_r = "(\\)|）)";

	private static Pattern chapter = Pattern.compile("" +
		"(?<AR>1、|2、|3、|4、|5、|6、|7、|8、|9、)" +
		"|" +
		"(?<HalfBracketAR>1）|2）|3）|4）|5）|6）|7）|8）|9）)" +
		"|" +
		"(?<CN>一、|二、|三、|四、|五、|六、|七、|八、|九、)" +
		"|" +
		"(?<BracketCN>" + bracket_l + "一" + bracket_r + "|" + bracket_l + "二" + bracket_r +
		"|" + bracket_l + "三" + bracket_r + "|" + bracket_l + "四" + bracket_r + "|" + bracket_l + "五" + bracket_r +
		"|" + bracket_l + "六" + bracket_r + "|" + bracket_l + "七" + bracket_r + "|" + bracket_l + "八" + bracket_r +
		"|" + bracket_l + "九" + bracket_r + ")"
	);

	public static Chapter read(List<Object> rawInfos) {// TODO bug 分节符号在同一个文章中被复用
		Chapter article = new Chapter(null);
		Chapter current = article;
		DoubleLinkedList<Pair<String, Chapter>> context = new DoubleLinkedList<>();
		StringBuilder cache = new StringBuilder();
		for (Object rawInfo : rawInfos) {
			if (rawInfo instanceof String) {
				for (String line : ((String) rawInfo).split("\n")) {
					line = StringUtil.trim(line);
					Matcher m = chapter.matcher(line).region(0, 6 < line.length() ? 6 : line.length());
					if (m.lookingAt()) {
						String type;
						if (m.group("AR") != null) {
							type = "AR";
						} else if (m.group("HalfBracketAR") != null) {
							type = "HalfBracketAR";
						} else if (m.group("CN") != null) {
							type = "CN";
						} else if (m.group("BracketCN") != null) {
							type = "BracketCN";
						} else {
							throw new RuntimeException("not support");
						}
						Chapter p = null;
						DoubleLinkedNode<Pair<String, Chapter>> node = context.getHead();
						while (node != null) {
							if (node.getValue().getLeft().equals(type)) {
								Chapter c = node.getValue().getRight();
								if (node.getPrev() == null) {
									context.clear();
								} else {
									context.tail(node.getPrev());
								}
								p = c.getParent();
								break;
							}
							node = node.getNext();
						}
						if (cache.length() > 0) {
							current.addContent(cache.toString());
							cache.setLength(0);
						}
						if (p == null) {
							current = new Chapter(type, current);
						} else {
							current = new Chapter(type, p);
						}
						current.getParent().addContent(current);
						context.add(new Pair<>(type, current));
					}
					cache.append(line).append("\n");
				}
			} else if (rawInfo instanceof HtmlTable) {
				if (cache.length() > 0) {
					current.addContent(cache.toString());
					cache.setLength(0);
				}
				current.addContent(rawInfo);
			}
		}
		if (cache.length() > 0) {
			current.addContent(cache.toString());
		}
		return article;
	}
}
